import {
  BadRequestException,
  Injectable,
  NestMiddleware,
  UnauthorizedException,
} from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import { config } from '@/config';
import { getUserFromToken } from '@/lib/jwt/token';
@Injectable()
export class authMiddleware implements NestMiddleware {
   async use(req: Request, res: Response, next: NextFunction) {
    try {
      const url = req.url;
      const { noAuthUrls } = config;
      if (!noAuthUrls.includes(url)) {
        
      }
      // 1：从header里获取：authorization
      const authorization = req.headers.authorization;
       
      // 获取token值:前后端要确认好，
      const token = authorization.split(' ')[1];
      console.log(token);
      const aa = await  getUserFromToken(token);
      console.log(aa);
        next(); 
    } catch (error) {}
  }
}
